package com.james.customcamera.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.james.customcamera.R;
import com.james.customcamera.ui.fragment.HomeFragment;
import com.james.customcamera.ui.fragment.MineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.main_container)
    FrameLayout mainContainerFl;
    @BindView(R.id.main_bottom_bar)
    RadioGroup mainBottomBarRg;
    @BindView(R.id.main_bottom_bar_home)
    RadioButton mainBottomBarHomeRb;
    @BindView(R.id.main_bottom_bar_camera)
    Button mainBottomBarCameraBtn;
    @BindView(R.id.main_bottom_bar_mine)
    RadioButton mainBottomBarMineRb;


    //fragment
    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    //当前fragment
    private Fragment mFragment;

    //记录用户首次点击返回键的时间
    private long firstTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.main_container, homeFragment).commit();
        mFragment = homeFragment;
    }

    private void initFragment() {

        mainBottomBarRg.setOnCheckedChangeListener(this);
        homeFragment = new HomeFragment();
        mineFragment = new MineFragment();
    }

    /**
     * 切换Fragment
     */
    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if (mFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有，则先把当前的Fragment隐藏，把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.main_container, fragment).commit();
            } else {
                //如果已经添加过，则先把当前的Fragment隐藏，把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }

    @OnClick(R.id.main_bottom_bar_camera)
    public void onViewClicked() {
        Intent intent = new Intent(MainActivity.this, CameraActivity.class);
        startActivity(intent);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_bottom_bar_home:
                mainBottomBarHomeRb.setChecked(true);
                mainBottomBarMineRb.setChecked(false);
                switchFragment(homeFragment);
                break;
            case R.id.main_bottom_bar_mine:
                mainBottomBarHomeRb.setChecked(false);
                mainBottomBarMineRb.setChecked(true);
                switchFragment(mineFragment);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出程序--->onKeyDown", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}


